﻿using MovieAPI.Models;
using System.Data.Entity;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Description;
using System.Web.Mvc;

namespace MovieAPI.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*"), RequireHttpsAttribute]
    public class DirectController : ApiController
    {
        private filmEntities db = new filmEntities();
        public IHttpActionResult GetDirectrByID(int id)
        {
            var DirectrInfo = db.DirectInfo.Where(A => A.DirectID == id).Include(a => a.Direct).Include(a => a.MovieInfo);
            return Ok(DirectrInfo.ToList());
        }
        public IHttpActionResult GetDirectrByMovieID(int id)
        {
            var DirectrInfo = db.DirectInfo.Where(A => A.MovieID == id).Include(a => a.Direct).Include(a => a.MovieInfo);
            return Ok(DirectrInfo.ToList());
        }

        [ResponseType(typeof(void))]
        public IHttpActionResult PutDirectInfo(dynamic Movie)
        {
            using (filmEntities db = new filmEntities())
            {
                int ID = Movie.ID;
                Direct direct = db.Direct.Where(M => M.DirectID == ID).FirstOrDefault();
                //MovieInfo.MovieAddress = Movie.Movie.MovieAddress;
                //MovieInfo.MovieName = Movie.Movie.MovieName;
                //MovieInfo.MovieDate = Movie.Movie.MovieDate;
                //MovieInfo.MovieImg = Movie.Movie.MovieImg;
                //MovieInfo.MovieTime = Movie.Movie.MovieTime;
                //MovieInfo.MovieType = Movie.Movie.MovieType;
                //MovieInfo.MovieReview = Movie.Movie.MovieReview;
                direct.DirectName = Movie.Movie.ActorName;
                direct.DirectImg = Movie.Movie.ActorImg;
                direct.Introduced = Movie.Movie.Introduced;
                direct.Birhday = Movie.Movie.Birhday;
                direct.Height = Movie.Movie.Height;
                direct.Nationality = Movie.Movie.Nationality;
                direct.Sex = Movie.Movie.Sex;
                db.SaveChanges();
                return CreatedAtRoute("DefaultApi", new { id = direct.DirectID }, direct);
            }
        }

        // POST: api/Actor
        [ResponseType(typeof(Direct))]
        public IHttpActionResult PostDirectInfo(dynamic Movie)
        {
            using (filmEntities db = new filmEntities())
            {
                Direct direct = new Direct();
                direct.DirectName = Movie.Movie.DirectName;
                direct.DirectImg = Movie.Movie.DirectImg;
                direct.Introduced = Movie.Movie.Introduced;
                direct.Birhday = Movie.Movie.Birhday;
                direct.Height = Movie.Movie.Height;
                direct.Nationality = Movie.Movie.Nationality;
                direct.Sex = Movie.Movie.Sex;
                db.Direct.Add(direct);
                db.SaveChanges();
                return CreatedAtRoute("DefaultApi", new { id = direct.DirectID }, direct);
            }
        }

        public IHttpActionResult GetAllActor()
        {
            using (filmEntities db = new filmEntities())
            {
                var directs = db.Direct.ToList();
                return Ok(directs);
            }
        }
        [System.Web.Http.HttpGet]
        public IHttpActionResult GetDirectByDirectID(int id)
        {
            using (filmEntities db = new filmEntities())
            {
                var direct = db.Direct.Where(a => a.DirectID == id).FirstOrDefault();
                return Ok(direct);
            }
        }
    }
}
